<template>
  <div>
    <ZgPage
      ref="page"
      title="表单管理"
      addBtnName="新增表单"
      :pageOpt="{showBatchBtn:false}"
      :addOpt="{title:'新增表单',width:'40%'}"
      :updateOpt="{title:'修改表单',width:'50%'}"
      @search="onSearch"
      @del="onDel"
      @add="onAdd"
      @update="onUpdate"
    >
      <template slot="columns">
        <a-table-column dataIndex="name" title="名称"></a-table-column>
        <a-table-column dataIndex="code" title="编码"></a-table-column>
        <a-table-column title="字段数量">
          <template slot-scope="text,record">
            <ColumnCount :formId="record.id"></ColumnCount>
          </template>
        </a-table-column>
        <a-table-column title="公式数量">
          <template slot-scope="text,record">
            <FormulaCount :formId="record.id"></FormulaCount>
          </template>
        </a-table-column>
        <a-table-column title="数据量">
          <template slot-scope="text,record">
            <DataCount :tableCode="record.code"></DataCount>
          </template>
        </a-table-column>
        <a-table-column title="附件管理">
          <template slot-scope="text,record">
            <a-tag v-if="record.hasAttach" size="small" type="success">开启</a-tag>
            <a-tag v-else size="small" type="danger">关闭</a-tag>
          </template>
        </a-table-column>
        <a-table-column dataIndex="validUrl" title="验证接口地址"></a-table-column>
        <a-table-column :width="300" dataIndex="tmplPath" title="模板文件路径">
          <template slot-scope="text,record">
            <div v-if="getTmplName(record.tmplPath).length>0">
              {{ getTmplName(record.tmplPath) }}<a class="ml10 link" @click="downloadTmpl(record.tmplPath)">下载</a>
            </div>
          </template>
        </a-table-column>
      </template>
      <template slot="add">
        <Add ref="add"></Add>
      </template>
      <template slot="update" slot-scope="scope">
        <Update ref="update" :jsonStr="scope.jsonStr"></Update>
      </template>
    </ZgPage>
  </div>
</template>
<script>
import Add from './Add'
import Update from './Update'
import ColumnCount from '../column/Count.vue'
import FormulaCount from '../formula/Count.vue'
import DataCount from './DataCount.vue'
export default {
  components: {
    Add, Update, ColumnCount, FormulaCount, DataCount
  },
  data () {
    return {
    }
  },
  methods: {
    getTmplName (tmplPath) {
      if (!tmplPath || tmplPath.length === 0) {
        return ''
      }
      try {
        const arr = JSON.parse(tmplPath)
        if (arr.length === 0) {
          return ''
        }
        return arr[0].name
      } catch (e) {
        return ''
      }
    },
    onSearch (param) {
      this.$http.get('/zg-autoform/form/page', param, data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    onDel (ids) {
      this.$http.post('/zg-autoform/form/del', ids, () => {
        this.$refs.page.search()
      })
    },
    onAdd () {
      const result = this.$refs.add.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-autoform/form/add', result.data, () => {
        this.$refs.page.hideAdd()
        this.$refs.page.search()
      })
    },
    onUpdate () {
      const result = this.$refs.update.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-autoform/form/update', result.data, () => {
        this.$refs.page.hideUpdate()
        this.$refs.page.search()
      })
    },
    downloadTmpl (jsonStr) {
      const json = JSON.parse(jsonStr)[0]
      this.$http.downloadMinio(this.MINIO_URL + json.path, json.name)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
